package com.sky.service.impl;

import com.sky.dto.DishDTO;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.service.DishFlavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public void addDishFlavor(DishFlavor dishFlavor) {
        dishFlavorMapper.addDishFlavor(dishFlavor);
    }

    @Override
    public List<DishFlavor> getDishFlavorById(Long id) {
        return dishFlavorMapper.getDishFlavorById(id);
    }

    @Override
    public void updateFlavor(DishDTO dishDTO) {
        Long id = dishDTO.getId();//菜品id
        ArrayList<Long> list1 = new ArrayList<>();
        list1.add(id);
        deleteDishFlavor(list1);//先删除
        List<DishFlavor> list = dishDTO.getFlavors();
        //新增
        for (DishFlavor flavor : list) {
            flavor.setDishId(id);
            dishFlavorMapper.addDishFlavor(flavor);
        }
    }

    @Override
    public void deleteDishFlavor(List<Long> ids) {
        dishFlavorMapper.deleteDishFlavorByIds(ids);
    }
}
